package com.netdisk.controller;

import com.netdisk.dto.request.file.CreateShareDTO;
import com.netdisk.dto.response.file.ShareDTO;
import com.netdisk.dto.response.file.ShareListDTO;
import com.netdisk.service.ShareService;
import com.netdisk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分享管理")
@RequestMapping("/share")
@RestController
public class ShareController {
    @Autowired
    private ShareService shareService;
    @ApiOperation(value = "创建分享")
    @PostMapping("/create")
    public Result<ShareDTO> createShare(CreateShareDTO createShareDTO) {
        ShareDTO shareDTO = shareService.createShare(createShareDTO);
        return Result.success(shareDTO);
    }

    @ApiOperation(value = "获取分享信息")
    @GetMapping("/info/{shareId}")
    public Result<ShareDTO> getShareInfo(@PathVariable String shareId) {
        ShareDTO shareDTO = shareService.getShareInfoByShareId(shareId);
        return Result.success(shareDTO);
    }

    @ApiOperation(value = "获取用户分享列表")
    @GetMapping("/list")
    public Result<ShareListDTO> getShareList(Integer userId) {
        ShareListDTO shareListDTO = new ShareListDTO();
        List<ShareDTO> list = shareService.getShareListById(userId);
        Integer total = shareService.getShareCountById(userId);
        shareListDTO.setTotal(total);
        shareListDTO.setList(list);
        return Result.success(shareListDTO);
    }

    @ApiOperation(value = "取消分享")
    @DeleteMapping("/cancel")
    public Result<Object> cancelShare(String shareId, Integer userId) {
        shareService.cancelShare(shareId, userId);
        return Result.success();
    }

}
